import os

import pygame

from lib.resources import ResourceManager

class Theme:
    def __init__(self, folder):
        self.name = "Default"
        self.comment = "The default PolyPlay theme."
        self.author = "Francesco Mastellone"
        self.licence = "Creative Commons"
        self.copyright = "Francesco Mastellone, 2009"
        
        self.path = os.path.join(os.path.abspath('data'), "theme", self.name)
        
        ResourceManager.load_folder_recursive(self.path)
        r = ResourceManager.cache
        
        self.font = r['DejaVuSans.ttf']
        
        self.buttonImage = r['button.png']
        self.buttonPressedImage = r['buttonPressed.png']
        
        self.gravityLabel = r['gravityLabel.png']
        self.frictionLabel = r['frictionLabel.png']
        self.springStrenghtLabel = r['springStrenghtLabel.png']
        self.groundFrictionLabel = r['groundFrictionLabel.png']
        
        self.saveWorldLabel = r['saveWorld.png']
        self.loadWorldLabel = r['loadWorld.png']
        
        self.selectLabel = r['selectLabel.png']
        self.moveLabel = r['moveLabel.png']
        
        self.massLabel = r['massLabel.png']
        self.constraintLabel = r['constraintLabel.png']
        self.clipLabel = r['clipLabel.png']
        self.structLabel = r['structLabel.png']
        self.lineLabel = r['lineLabel.png']
        
        self.barLeftImage = r['barLeft.png']
        self.barCenterImage = r['barCenter.png']
        self.barRightImage = r['barRight.png']
        self.barInsetImage = r['barInset.png']
        
        self.plusImage = r['plus.png']
        self.bigPlusImage = r['bigPlus.png']
        self.minusImage = r['minus.png']
        
        #TABS
        tabImageNames = ("topLeft.png", "top.png", "topRight.png",
                         "right.png", "bottomRight.png", "bottom.png",
                         "bottomLeft.png", "left.png")
        self.tabImages = tuple(r[name] for name in tabImageNames)
    
    def render_button(self, w, h):
        return self.buttonImage[0]
    
    def render_button_hovered(self, w, h):
        return self.buttonImage[0]
    
    def render_button_pressed(self, w, h):
        return self.buttonPressedImage[0]
    
    def render_knob(self, w, h):
        return self.buttonImage[0]
    
    def render_knob_hovered(self, w, h):
        return self.buttonImage[0]
    
    def render_tab(self, w, h):
        """Returns pygame.Surface, pygame.Rect
        
        nw, n, ne, e, se, s, sw, w
        0   1  2   3  4   5  6   7
        """
        surface = pygame.Surface((w, h), pygame.SRCALPHA, 32)
        
        img = self.tabImages
        
        #Corners
        tlw, tlh = img[0][1].size
        trw, trh = img[2][1].size
        brw, brh = img[4][1].size
        blw, blh = img[6][1].size
        
        surface.blit(img[0][0], (0, 0))
        surface.blit(img[2][0], (w - trw, 0))
        surface.blit(img[4][0], (w - trw, h - brh))
        surface.blit(img[6][0], (0, h - blh))
        
        #Borders
        th = img[1][1].height
        rw = img[3][1].width
        bh = img[5][1].height
        lw = img[7][1].width
        
        ti = img[1][0]
        ri = img[3][0]
        bi = img[5][0]
        li = img[7][0]
        
        for i in range(tlw, w - trw):
            surface.blit(ti, (i, 0))
        for i in range(blw, w - brw):
            surface.blit(bi, (i, h-bh))
        for i in range(tlh, h - blh):
            surface.blit(li, (0, i))
        for i in range(trh, h - brh):
            surface.blit(ri, (w-rw, i))
        
        return surface
